do "E:\yungyu\lottery_baby\do\set_environment.do"

clear
set more off

global control1 "i.pers_gender i.houhld_resid_cd pers_income_pre1 pers_asset_pre1 pers_earnings_pre1 pers_labor_pre1 married_pre1"
global control2 "totbaby_pre1"
global control3 "pre*redeem*"

use "$wdata\project_lottery_fertility_b3a6_first_5k.dta", clear

replace norm_year = norm_year + 3		

cap rm "$table/temp/FigD2.txt"
cap rm "$table/temp/FigD2.xls"

foreach y in totbaby {
	forv upper = 0.5(0.5)15{
		reghdfe `y' currentxprizexp* prize10M c.prize10M#i.norm c.prize10M#1.current i.age $control1 $control2 $control3 if prize10M <= `upper', a(norm#current year) cl(houseid)
		outreg2 using "$table/temp/FigD2.xls" , append bd(3) sd(3) nocon keep(currentxprize*) ctitle(`upper')
	}
}

import delimited using "$table/temp/FigD2.txt", clear
export excel "$table/Tables.xlsx", sheet("FigD2") sheetreplace


import excel using "$table/Tables.xlsx", clear sheet(FigD2) first

keep if _n == 2 | _n == 20 | _n == 21
sxpose, clear

drop in 1
rename _var1 max
rename _var2 b
rename _var3 se

foreach y of varlist max b se{
	replace `y' = subinstr(`y',"*","",.)
	replace `y' = subinstr(`y',"(","",.)
	replace `y' = subinstr(`y',")","",.)
	destring `y', replace
}

replace b = b
replace se= se

gen upper = b + 1.96 * se
gen lower = b - 1.96 * se

replace max = max * 10

twoway 	(connect b max, color(black)) ///
		(rline upper lower max, lp(dash) color(gs8)), ///
		legend(order(1 "Point Estimate" 2 "95% CI")) ///
		xtitle("Maximum Prize Amount (Million NT$)") xlabel(20(20)140) ///
		ytitle("Effect of a Five Million NT$ Lottery Prize on Fertility") ylabel(0(0.03)0.12, angle(0) format(%4.2f)) ///
		scheme(s1color) yline(0, lc(black))
graph export "$folder/figure/FigC3.png", as(png) replace
graph export "$folder/figure/FigC3.eps", as(eps) replace
